import os
import csv

# 获取脚本当前所在目录
folder_path = os.path.dirname(os.path.abspath(__file__))

for filename in os.listdir(folder_path):
    if filename.endswith('.csv'):
        csv_path = os.path.join(folder_path, filename)
        txt_path = os.path.join(folder_path, filename.replace('.csv', '.txt'))
        
        with open(csv_path, 'r', encoding='utf-8') as csvfile, open(txt_path, 'w', encoding='utf-8') as txtfile:
            reader = csv.DictReader(csvfile)
            for row in reader:
                role = row.get('Role', '').strip()
                text = row.get('Text', '').strip()
                # 跳过1. role和text都为空 2. role为null且text为空
                if (role == '' and text == '') or (role.lower() == 'null' and text == ''):
                    continue
                if role == 'zhuchiren.wav':
                    role = '主持人'
                elif role == 'beifangzhe.wav':
                    role = '被访者'
                txtfile.write(f"{role}：{text}\n")

print("转换完成！")